package com.example.piece.ext

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import android.telephony.TelephonyManager
import android.widget.TextView
import com.example.base.BaseApp
import com.example.base.BuildConfig
import java.io.IOException
import java.io.InputStreamReader
import java.io.LineNumberReader
import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern


/**
 * 快捷描述选择弹窗
 * @param replyType 快捷描述的类型
 * @param onSelectListener 当用户选择描述时的回调
 */
fun Any.setExternalFont(
    textView: TextView, isBold: Boolean
) {

    val assets = BaseApp.context.assets
    val createFromAsset = Typeface.createFromAsset(assets, "DIN-MEDIUM.OTF")
    textView.setTypeface(createFromAsset)
    if (isBold) {
        textView.paint.isFakeBoldText = true
    }
}



/**
 * 序列号
 *
 * @return
 */
fun Any.getSerialNumber(): String {
    var serial = ""

     if (BuildConfig.DEBUG) {
         serial = "sgcs111"

    } else {
         try {
             val c = Class.forName("android.os.SystemProperties")
             val get = c.getMethod("get", String::class.java)
             serial = get.invoke(c, "ro.serialno") as String
         } catch (e: Exception) {
             e.printStackTrace()
         }
    }


    return serial
}

/**
 * 获取设备MAC地址
 *
 * 需添加权限 `<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>`
 *
 * @return MAC地址
 */
fun Any.getMacAddress(): String {
    var macAddress: String = ""
    var lnr: LineNumberReader? = null
    var isr: InputStreamReader? = null
    try {
        val pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address")
        isr = InputStreamReader(pp.inputStream)
        lnr = LineNumberReader(isr)
        macAddress = lnr.readLine().replace(":", "")
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
    }
    return macAddress
}

 fun Any.isMobileNO(mobiles: String?): Boolean {
  /*  val p: Pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$")
    val m: Matcher = p.matcher(mobiles)
    return m.matches()*/

       val p: Pattern = Pattern.compile("^1\\d{10}\$");
  val m: Matcher = p.matcher(mobiles)
  return m.matches()
}


/**
 * 获取当前手机系统语言。
 *
 * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
 */
fun Any.getSystemLanguage(): String? {
    return Locale.getDefault().getLanguage()
}

/**
 * 获取当前系统上的语言列表(Locale列表)
 *
 * @return  语言列表
 */
fun Any.getSystemLanguageList(): Array<Locale?>? {
    return Locale.getAvailableLocales()
}

/**
 * 获取当前手机系统版本号
 *
 * @return  系统版本号
 */
fun Any.getSystemVersion(): String? {
    return Build.VERSION.RELEASE
}

/**
 * 获取手机型号
 *
 * @return  手机型号
 */
fun Any.getSystemModel(): String? {
    return Build.MODEL
}

/**
 * 获取手机厂商
 *
 * @return  手机厂商
 */
fun Any.getDeviceBrand(): String? {
    return Build.BRAND
}

/**
 * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
 *
 * @return  手机IMEI
 *//*
fun getIMEI(ctx: Context): String? {
    val tm =
        ctx.getSystemService(Activity.TELEPHONY_SERVICE) as TelephonyManager
    return tm?.deviceId
}  */



